package com.gdsz.libbasic.extensions

import android.graphics.LinearGradient
import android.graphics.Shader
import android.graphics.drawable.Drawable
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.core.view.isVisible
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
//import me.jessyan.autosize.utils.AutoSizeUtils
import java.lang.ref.WeakReference
import kotlin.math.ceil


/**
 * 设置编辑
 * @receiver View
 * @param l Int
 * @param t Int
 * @param r Int
 * @param b Int
 */
//fun View.setMargin(l: Int = 0, t: Int = 0, r: Int = 0, b: Int = 0) {
//    if (this.layoutParams is MarginLayoutParams) {
//        val p = this.layoutParams as MarginLayoutParams
//        p.setMargins(l.toDp(context), t.toDp(context), r.toDp(context), b.toDp(context))
//        this.requestLayout()
//    }
//}

//fun Int.toDp(context: Context): Int {
//    return AutoSizeUtils.dp2px(context, this.toFloat())
//}
//
//fun Float.toDp(context: Context): Int {
//    return AutoSizeUtils.dp2px(context, this)
//}
//
//fun Int.toSp(context: Context): Int {
//    return AutoSizeUtils.sp2px(context, this.toFloat())
//}
//
//fun Float.toSp(context: Context): Int {
//    return AutoSizeUtils.sp2px(context, this)
//}

fun View.setSize(width: Int =-100, height: Int=-100) {
    val layoutParams: ViewGroup.LayoutParams = this.layoutParams
    if (width != -100){
        layoutParams.width = width
    }
    if (height!=-100){
        layoutParams.height = height
    }

    this.layoutParams = layoutParams
}

//fun ViewGroup.setPadding(l: Int = 0, t: Int = 0, r: Int = 0, b: Int = 0) {
//    this.setPadding(l.toDp(context), t.toDp(context), r.toDp(context), b.toDp(context))
//    this.requestLayout()
//}

fun View.setVisible() {
    if (!isVisible) {
        this.visibility = View.VISIBLE
    }
}

fun View.setGone() {
    if (isVisible) {
        this.visibility = View.GONE
    }

}

fun View.setInVisible() {
    this.visibility = View.INVISIBLE
}


fun ImageView.srcImage(path: String?) {
    val context = WeakReference(this.context)
    context.get()?.let {

        Glide.with(it).load(path).diskCacheStrategy(DiskCacheStrategy.ALL).skipMemoryCache(false)
            .listener(object : RequestListener<Drawable> {



                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Drawable>,
                    isFirstResource: Boolean
                ): Boolean {
                    return false
                }

                override fun onResourceReady(
                    resource: Drawable,
                    model: Any,
                    target: Target<Drawable>?,
                    dataSource: DataSource,
                    isFirstResource: Boolean
                ): Boolean {
                    return false
                }

            }).into(this)
    }
}


/**
 * 设置视图的形状背景
 *
 * @param solidColor 固定颜色，默认为透明
 * @param radius 边角半径，默认为0
 * @param borderWidth 边框宽度，默认为0
 * @param borderColor 边框颜色，默认为透明
 * @param cornerRadii 角半径数组，默认为null
 * @param colors 渐变颜色数组，默认为null
 * @param orientation 渐变方向，默认为null
 */
//fun View.setShapeBackground(
//    @ColorInt solidColor: Int = Color.TRANSPARENT,
//    radius: Int = 0,
//    borderWidth: Int = 0,
//    borderColor: Int = Color.TRANSPARENT,
//    cornerRadii: FloatArray? = null,
//    @ColorInt colors: IntArray? = null,
//    orientation: GradientDrawable.Orientation? = null
//) {
//    val shape = GradientDrawable()
//    if (radius != 0) {
//        shape.cornerRadius = radius.toDp(this.context).toFloat()
//    }
//    cornerRadii?.let {
//        shape.cornerRadii = it
//    }
//    if (colors == null) {
//        shape.setColor(solidColor)
//    } else {
//        shape.colors = colors
//        shape.orientation = orientation
//    }
//
//    shape.setStroke(borderWidth.toDp(this.context), borderColor)
//    background = shape
//}


/**
 * 设置文本的渐变背景
 *
 * @param colors 渐变颜色数组
 * @param positions 渐变颜色的位置数组
 */
fun TextView.setTextGradient(@ColorInt colors: IntArray, positions: FloatArray) {

    val fm = this.paint.fontMetrics
    val height = ceil((fm.descent - fm.ascent).toDouble()).toFloat()

    // 创建渐变背景
    val gradientDrawable = LinearGradient(
        0f, 0f, 0f, height, colors, positions, Shader.TileMode.CLAMP
    )
    this.paint.shader = gradientDrawable
    //background = gradientDrawable
}
















